import request from "@/utils/request";
import type {
  GetSpuListApiParams,
  GetSpuListApiResponse,
  BaseSaleAttrList,
  AddOrUpdateSpuParams,
  SpuImageList,
  SpuSaleAttrList,
} from "./model/spuModel";

const enum Api {
  getSpuList = "/admin/product",
  getBaseSaleAttrList = "/admin/product/baseSaleAttrList",
  addSpu = "/admin/product/saveSpuInfo",
  getSpuImageList = "/admin/product/spuImageList",
  getSpuSaleAttrList = "/admin/product/spuSaleAttrList",
  updateSpu = "/admin/product/updateSpuInfo",
}

/**
 * 获取SPU列表
 * @param GetSpuListApiParams
 * @returns GetSpuListApiResponse
 */
export const getSpuListApi = ({
  page,
  limit,
  category3Id,
}: GetSpuListApiParams) => {
  return request.get<any, GetSpuListApiResponse>(
    Api.getSpuList + `/${page}/${limit}`,
    {
      params: {
        category3Id,
      },
    }
  );
};

/**
 * 获取基础销售属性列表
 * @returns BaseSaleAttrList
 */
export const getBaseSaleAttrListApi = () => {
  return request.get<any, BaseSaleAttrList>(Api.getBaseSaleAttrList);
};

/**
 * 添加SPU
 * @param data AddOrUpdateSpuParams
 * @returns null
 */
export const addSpuApi = (data: AddOrUpdateSpuParams) => {
  return request.post<any, null>(Api.addSpu, data);
};

/**
 * 获取Spu图片列表
 * @param id spuId
 * @returns SpuImageList
 */
export const getSpuImageListApi = (id: number) => {
  return request.get<any, SpuImageList>(Api.getSpuImageList + `/${id}`);
};

/**
 * 获取Spu销售属性列表
 * @param id spuId
 * @returns SpuSaleAttrList
 */
export const getSpuSaleAttrListApi = (id: number) => {
  return request.get<any, SpuSaleAttrList>(Api.getSpuSaleAttrList + `/${id}`);
};

/**
 * 更新spu
 * @param data AddOrUpdateSpuParams
 * @returns null
 */
export const updateSpu = (data: AddOrUpdateSpuParams) => {
  return request.post<any, null>(Api.updateSpu, data);
};
